package com.lry.netty01.server.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 日期工具
 */
public class DateUtil {

    public static Date getBefore15Min() {
        Calendar can = Calendar.getInstance();
        can.add(Calendar.MINUTE, -15);
        return can.getTime();
    }

    private final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    private final static SimpleDateFormat sdfTimes = new SimpleDateFormat("yyyyMMddHHmmss");
    private final static SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String formatDate(Date date ) {
        try {
            if(date==null){
                return null;
            }
            return yyyyMMddHHmmss.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String formatDateFromStr(String time) {
        try {
            Date date = sdfTimes.parse(time);
            return sdfTime.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }

    public static String getHexTime(Date date){
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();

        int year = localDateTime.getYear()%100;
        int month = localDateTime.getMonthValue();
        int day = localDateTime.getDayOfMonth();
        int hour = localDateTime.getHour();
        int minute = localDateTime.getMinute();
        int second = localDateTime.getSecond();
        int millisecond = second*1000;

        StringBuffer sub = new StringBuffer();
        String millisecondHexStr = ByteUtil.intToHex4(millisecond);

        sub.append(millisecondHexStr).
                append(ByteUtil.intToHex(minute)).
                append(ByteUtil.intToHex(hour)).
                append(ByteUtil.intToHex(day)).
                append(ByteUtil.intToHex(month)).
                append(ByteUtil.intToHex(year));

        return sub.toString().toUpperCase();
    }


    public static String getLastMonthHexTime(){
        Calendar cal = Calendar.getInstance();
        cal.add(cal.DATE, -30);
        int year = cal.get(Calendar.YEAR)%100;
        int month = cal.get(Calendar.MONTH)+1;
        int date = cal.get(Calendar.DATE);
        int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        int millisecond = cal.get(Calendar.MILLISECOND)+second*1000;
        StringBuffer sub = new StringBuffer();

        String millisecondHexStr = ByteUtil.intToHex4(millisecond);

        sub.append(millisecondHexStr).
                append(ByteUtil.intToHex(minute)).
                append(ByteUtil.intToHex(hourOfDay)).
                append(ByteUtil.intToHex(date)).
                append(ByteUtil.intToHex(month)).
                append(ByteUtil.intToHex(year));

        return sub.toString().toUpperCase();

    }

    public static Date get2000Time(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse("2000-01-01 00:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getLastDayHexTime(){
        Calendar cal = Calendar.getInstance();
        cal.add(cal.DATE, -1);
        int year = cal.get(Calendar.YEAR)%100;
        int month = cal.get(Calendar.MONTH)+1;
        int date = cal.get(Calendar.DATE);
        int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        int millisecond = cal.get(Calendar.MILLISECOND)+second*1000;
        StringBuffer sub = new StringBuffer();

        String millisecondHexStr = ByteUtil.intToHex4(millisecond);

        sub.append(millisecondHexStr).
                append(ByteUtil.intToHex(minute)).
                append(ByteUtil.intToHex(hourOfDay)).
                append(ByteUtil.intToHex(date)).
                append(ByteUtil.intToHex(month)).
                append(ByteUtil.intToHex(year));

        return sub.toString().toUpperCase();
    }
    public static String getCurHexTime(){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR)%100;
        int month = cal.get(Calendar.MONTH)+1;
        int date = cal.get(Calendar.DATE);
        int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        int millisecond = cal.get(Calendar.MILLISECOND)+second*1000;
        StringBuffer sub = new StringBuffer();

        String millisecondHexStr = ByteUtil.intToHex4(millisecond);

        sub.append(millisecondHexStr).
                append(ByteUtil.intToHex(minute)).
                append(ByteUtil.intToHex(hourOfDay)).
                append(ByteUtil.intToHex(date)).
                append(ByteUtil.intToHex(month)).
                append(ByteUtil.intToHex(year));

        return sub.toString().toUpperCase();
    }
    public static String getCurHexTime1(){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR)%100;
        int month = cal.get(Calendar.MONTH)+1;
        int date = cal.get(Calendar.DATE);
        int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        int millisecond = cal.get(Calendar.MILLISECOND)+second*1000;
        StringBuffer sub = new StringBuffer();

        String millisecondHexStr = ByteUtil.intToHex4(millisecond);

        sub.append(millisecondHexStr).
                append(ByteUtil.intToHex(minute)).
                append(ByteUtil.intToHex(hourOfDay)).
                append(ByteUtil.intToHex(date)).
                append(ByteUtil.intToHex(month)).
                append(ByteUtil.intToHex(year));

        return sub.toString().toUpperCase();
    }


    //ms(2),分，时，日，月，年(%100)
    public static LocalDateTime getTimeForByteTimeYx(byte[] byteTime){
        if(null==byteTime||byteTime.length!=7){
            return null;
        }
        return LocalDateTime.of(Integer.parseInt("20"+byteTime[6]),byteTime[5],byteTime[4],byteTime[3],byteTime[2],0);
    }

    public static LocalDateTime getTimeForByteTime(byte[] byteTime){
        if(null==byteTime||byteTime.length!=7){
            return null;
        }

        return LocalDateTime.of(Integer.parseInt("20"+byteTime[6]),byteTime[5],byteTime[4],byteTime[3],byteTime[2],0);
    }

    public static String getTimeForHexTime(String hexTime){
        if(hexTime==null){
            return null;
        }
        hexTime = ByteUtil.reverseStr(hexTime,true);
        String year = ByteUtil.hexToIntStr(hexTime.substring(0,2));
        String month = ByteUtil.hexToIntStr(hexTime.substring(2,4));
        String day = ByteUtil.getFormatStr((Integer.parseInt(hexTime.substring(4,6),16)&0x1f)+"",2);
        String hour = ByteUtil.hexToIntStr(hexTime.substring(6,8));
        String minute = ByteUtil.hexToIntStr(hexTime.substring(8,10));
        int second = Integer.parseInt(hexTime.substring(10,14),16);
        int millisecond = second%1000;
        second = second/1000;
        StringBuffer sub = new StringBuffer();
        sub.append(year).append(month).append(day).append(hour).append(minute).append(ByteUtil.getFormatStr(second+"",2)).append(ByteUtil.getFormatStr(millisecond+"",3));
        return sub.toString();
    }

    public static void main(String[] args) throws Exception {
//       String date = "84D3280E630112";
//       System.out.println(getTimeForHexTime(date));
//       byte[] b = new byte[2];
//       b[0] = (byte)132;
//       b[1] = (byte)211;
//
//       int int1=(b[0]&0xff)<<8;
//       int int2=(b[1]&0xff);
//
//       System.out.println(int1 | int2);


        getHexTime(new Date());
    }
}
